package com.longyn.sensor;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import com.lyn.serialPort.Port;
import com.lyn.utils.ByteUtil;
import com.lyn.utils.CRC16Util;

import java.util.HashSet;

/**
 * 达林科技 温湿度传感器 型号 DL10-MTTL-V05-S1
 * @author longyn 2023/6/10
 * @version 1.0.0
 */
public class DLTempAndHumiditySensor implements TempAndHumiditySensor, Runnable {

    private final Handler mainHandler = new Handler(Looper.getMainLooper());
    private Handler threadHandler;
    private HandlerThread handlerThread;
    private final HashSet<TempAndHumidityListener> tempAndHumidityListenerHashSet = new HashSet<>();
    private Port port;

    @Override
    public void init(Port port) {
        if (port == null)return;
        this.port = port;
        this.handlerThread = new HandlerThread("DLTempAndHumiditySensorThread");
        this.handlerThread.start();
        threadHandler = new Handler(handlerThread.getLooper());
        threadHandler.post(this);
    }

    @Override
    public void destroy() {
        if (threadHandler !=null){
            threadHandler.removeCallbacksAndMessages(null);
        }
        if (handlerThread!=null){
            handlerThread.quitSafely();
        }
        if (port!=null){
            port.close();
        }
    }

    @Override
    public float[] getCurrentTempAndHumidity() {
        try {
            byte temp_cmd = 0x03;
            return cmd(temp_cmd);
        }catch (Throwable e){
            e.printStackTrace();
        }
        return new float[2];
    }

    @Override
    public void registerTempAndHumidityListener(TempAndHumidityListener tempAndHumidityListener) {
        this.tempAndHumidityListenerHashSet.add(tempAndHumidityListener);
    }

    @Override
    public void unRegisterTempAndHumidityListener(TempAndHumidityListener tempAndHumidityListener) {
        this.tempAndHumidityListenerHashSet.remove(tempAndHumidityListener);
    }

    @Override
    public void run() {
        try {
            byte temp_cmd = 0x03;
            float[] floats = cmd(temp_cmd);
            mainHandler.post(() -> {
                for (TempAndHumidityListener listener:tempAndHumidityListenerHashSet){
                    listener.onTempAndHumidity(floats[0]/10f,floats[1]/10f);
                }
            });
        }catch (Throwable e){
            e.printStackTrace();
        }finally {
            threadHandler.postDelayed(this,300);
        }
    }

    private float[] cmd(byte cmd) throws Throwable{
        float[] floats = new float[2];
        byte[] data = new byte[8];
        //设备地址
        data[0] = 0x01;
        //功能码
        data[1] = cmd;
        //寄存器地址
        data[2] = 0x04;
        data[3] = 0x0A;
        //寄存器数量
        data[4] = 0x00;
        data[5] = 0x02;
        int calcCrc16 = CRC16Util.calcCrc16(data, 0, 6);
        //crc校验
        data[6] = (byte) (calcCrc16);
        data[7] = (byte) (calcCrc16>>8);
        byte[] send = port.send(data, 500);
        if (send!=null&&send.length>3){
            byte[] tempData = new byte[2];
            System.arraycopy(send,3,tempData,0,tempData.length);
            byte[] humidityData = new byte[2];
            System.arraycopy(send,5,humidityData,0,humidityData.length);
            floats[0] = Float.parseFloat(ByteUtil.binary(tempData, 10));
            floats[1] = Float.parseFloat(ByteUtil.binary(humidityData, 10));
        }
        return floats;
    }
}
